package common.util;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.storage.model.FileListing;
import com.qiniu.util.Auth;

import java.io.File;
import java.io.IOException;


/**
 * Created by tangxinmao on 2016/11/24.
 */
public class UploadUtil {
    //要上传的空间
    private static String bucketName;
    //密钥配置
    private static Auth auth;

    ///////////////////////指定上传的Zone的信息//////////////////
    //第一种方式: 指定具体的要上传的zone
    //注：该具体指定的方式和以下自动识别的方式选择其一即可
    //要上传的空间(bucket)的存储区域为华东时
    // Zone z = Zone.zone0();
    //要上传的空间(bucket)的存储区域为华北时
    // Zone z = Zone.zone1();
    //要上传的空间(bucket)的存储区域为华南时
    // Zone z = Zone.zone2();

    //第二种方式: 自动识别要上传的空间(bucket)的存储区域是华东、华北、华南。
    private static Zone z = Zone.zone1();
    private static Configuration configuration = new Configuration(z);

    //创建上传对象
    private static UploadManager uploadManager = new UploadManager(configuration);
    //创建空间对象
    private static BucketManager bucketManager;


    public static void main(String args[]) throws IOException {
        download();
    }

    /**
     * 上传文件
     *
     * @param file
     * @throws IOException
     */
    public static void upload(File file) throws IOException {
        try {
            //调用put方法上传
            Response res = uploadManager.put(file.getAbsolutePath(), file.getName(), auth.uploadToken(bucketName));
            //打印返回的信息
            System.out.println(res.bodyString());
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            System.out.println(r.toString());
            try {
                //响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                //ignore
            }
        } finally {
            file.delete();
        }
    }

    /**
     * 获取文件目录
     *
     * @return
     * @throws QiniuException
     */
    public static FileInfo[] listFiles() throws QiniuException {
        FileListing fileListing = bucketManager.listFiles(bucketName, null, null, 100, null);
        FileInfo[] items = fileListing.items;
        return items;
    }

    /**
     * 下载文件
     */
    public static void download() {
        //调用privateDownloadUrl方法生成下载链接,第二个参数可以设置Token的过期时间
        String downloadRUL = auth.privateDownloadUrl("url");
        System.out.println(downloadRUL);
    }

    public static String getBucketName() {
        return bucketName;
    }

    public static void setBucketName(String bucketName) {
        UploadUtil.bucketName = bucketName;
    }

    public static Auth getAuth() {
        return auth;
    }

    public static void setAuth(Auth auth) {
        UploadUtil.auth = auth;
    }

    public static BucketManager getBucketManager() {
        return bucketManager;
    }

    public static void setBucketManager(BucketManager bucketManager) {
        UploadUtil.bucketManager = bucketManager;
    }

    public static Configuration getConfiguration() {
        return configuration;
    }

    public static void setConfiguration(Configuration configuration) {
        UploadUtil.configuration = configuration;
    }
}
